<?php
include($_SERVER['DOCUMENT_ROOT'] . '/static/db/connect.php');
// 获取毫秒值时间戳的函数
function getMillisecond()
{
    list($t1, $t2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}

$timestamp = getMillisecond();
if (isset($_POST["authCode"])) {
    $authCode = $_POST["authCode"];
    $sql = "select auth_id from auth_code  WHERE auth_code = '$authCode'";
    $mysqli_result = $mysqli->query($sql);
    $info = $mysqli_result->fetch_assoc();
    if (isset($info)) {
        $token = md5($timestamp . $authCode);
        setcookie("token", $token, 0, "/", "tiann90.com", false, true);
        $timestamp_expire = $timestamp + 3600 * 1000;
        $sql = "insert into auth_token (token, timestamp) VALUES ('$token', $timestamp_expire)";
        $mysqli->query($sql);
        echo 1;
    } else {
        echo "Can't find authCode.";
    }
} elseif (isset($_POST["token"])) {
    $token = $_POST["token"];
    $sql = "select timestamp from auth_token  WHERE token = '$token'";
    $mysqli_result = $mysqli->query($sql);
    $info = $mysqli_result->fetch_assoc();
    if ($info['timestamp'] > $timestamp)
        echo 1;
    else
        echo "The verification code has expired.";
} else {
    echo 404;
}

$mysqli->close();
?>